Skip to main content

Events

Key Takeaways:

  • Events in smart contracts serve as a communication mechanism between the contract and user interfaces;
  • By emitting events, smart contracts can notify external applications or listeners about specific actions or changes;
  • Events play a key role in facilitating effective interaction with user interfaces while ensuring secure automated execution of code-based agreements.

Example of events

Notice the Logs section near the Transaction Details. ESDTNFTCreate

This section shows all the events this transaction emitted. Notice that most transactions have this Logs section.

ESDTNFTTransfer

Let's open this tab and inspect the Events

ESDTNFTTransfer Event

Notice there are 4 data fields:

  • SBPT-774fbd - the token ID;
  • 1 - the nonce;
  • 1 - the value (the Smart display feature doesn't know how to interpret that but the Decimal option does);
  • erd1ld6er5zpdze3cynzkapur9qhzh826jje6n87g7tvdfrtszs8jn2qv44nqd - the destination address.

Let's inspect a more complex operation and check all the log events. Here is the link to the transaction.

Swap Transaction

Notice that the transaction has 4 transfers and 1 burn operations. Let's inspect the logs:

Swap Event1

The first transaction is an ESDTTransfer for the token USDC-c76f1f which has 6 decimals; therefore 580345261 is equal to 580,345261$. You can match the events to the Token Operations from the Transacton Details.

You can check the deposit_swap_fees_event code here.

Notice the burn event below. Keep in mind that MEX-455c57 has 18 decimals. Swap Event2

Here is the code for the swap_no_fee_and_forward event.

Writing your own event in the Smart Contract

The structure of an event is:

    #[event("your_event_name")]
fn the_function_you_call_when_you_want_to_emit_en_event(
&self,
#[indexed] field1: &TokenIdentifier,
#[indexed] field2: &ManagedAddress,
#[indexed] field3: u64,
#[indexed] field4: BigUint,
);

And when you want to emit an event, just call the function:

    self.the_function_you_call_when_you_want_to_emit_en_event(field1, field2, field3, field4);